home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / Net / DNS / RR.php < prev   
PHP Script  |  2004-03-24  |  9KB  |  290 lines

  1. <?php
  2. /*
  3.  *  License Information:
  4.  *
  5.  *    Net_DNS:  A resolver library for PHP
  6.  *    Copyright (C) 2002 Eric Kilfoil eric@ypass.net
  7.  *
  8.  *    This library is free software; you can redistribute it and/or
  9.  *    modify it under the terms of the GNU Lesser General Public
  10.  *    License as published by the Free Software Foundation; either
  11.  *    version 2.1 of the License, or (at your option) any later version.
  12.  *
  13.  *    This library is distributed in the hope that it will be useful,
  14.  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  *    Lesser General Public License for more details.
  17.  *
  18.  *    You should have received a copy of the GNU Lesser General Public
  19.  *    License along with this library; if not, write to the Free Software
  20.  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  21.  */
  22.  
  23. /* Include files {{{ */
  24. require_once("$phpdns_basedir/DNS/RR/A.php");
  25. require_once("$phpdns_basedir/DNS/RR/NS.php");
  26. require_once("$phpdns_basedir/DNS/RR/CNAME.php");
  27. require_once("$phpdns_basedir/DNS/RR/PTR.php");
  28. require_once("$phpdns_basedir/DNS/RR/SOA.php");
  29. require_once("$phpdns_basedir/DNS/RR/MX.php");
  30. require_once("$phpdns_basedir/DNS/RR/TSIG.php");
  31. /* }}} */
  32. /* Net_DNS_RR object definition {{{ */
  33. /**
  34.  * Resource Record object definition
  35.  *
  36.  * Builds or parses resource record sections of the DNS  packet including
  37.  * the answer, authority, and additional  sections of the packet.
  38.  *
  39.  * @package Net_DNS
  40.  */
  41. class Net_DNS_RR
  42. {
  43.     /* class variable definitions {{{ */
  44.     var $name;
  45.     var $type;
  46.     var $class;
  47.     var $ttl;
  48.     var $rdlength;
  49.     var $rdata;
  50.     /* }}} */
  51.  
  52.     /*
  53.      * I finally did it... i pass an array to the function
  54.      * instead of a parameter list... UGH... i hate perl...
  55.      */
  56.     /* class constructor - Net_DNS_RR($rrdata) {{{ */
  57.     function Net_DNS_RR($rrdata)
  58.     {
  59.         if (is_string($rrdata)) {
  60.             $this = $this->new_from_string($rrdata);
  61.         } else if (count($rrdata) == 7) {
  62.             list ($name, $rrtype, $rrclass, $ttl, $rdlength, $data, $offset) = $rrdata;
  63.             $this = $this->new_from_data($name, $rrtype, $rrclass, $ttl, $rdlength, $data, $offset);
  64.         } else {
  65.             $this = $this->new_from_array($rrdata);
  66.         }
  67.     }
  68.  
  69.     /* }}} */
  70.     /* Net_DNS_RR::new_from_data($name, $ttl, $rrtype, $rrclass, $rdlength, $data, $offset) {{{ */
  71.     function new_from_data($name, $rrtype, $rrclass, $ttl, $rdlength, $data, $offset)
  72.     {
  73.         $this->name = $name;
  74.         $this->type = $rrtype;
  75.         $this->class = $rrclass;
  76.         $this->ttl = $ttl;
  77.         $this->rdlength = $rdlength;
  78.         $this->rdata = substr($data, $offset, $rdlength);
  79.         if (class_exists("Net_DNS_RR_" . $rrtype)) {
  80.             $scn = "Net_DNS_RR_" . $rrtype;
  81.             $subclass = new $scn($this, $data, $offset);
  82.             return($subclass);
  83.         } else {
  84.             return($this);
  85.         }
  86.     }
  87.  
  88.     /* }}} */
  89.     /* Net_DNS_RR::new_from_string($rrstring, $update_type = "") {{{ */
  90.     function new_from_string($rrstring, $update_type = "")
  91.     {
  92.         $ttl = 0;
  93.         $parts = preg_split("/[\s]+/", $rrstring);
  94.         while ($s = array_shift($parts)) {
  95.             if (!strlen($name)) {
  96.                 $name = ereg_replace("\.+$", "", $s);
  97.             } else if (preg_match("/^\d+$/", $s)) {
  98.                 $ttl = $s;
  99.             } else if (!strlen($rrclass) && ! is_null(Net_DNS::classesbyname(strtoupper($s)))) {
  100.                 $rrclass = strtoupper($s);
  101.                 $rdata = join(" ", $parts);
  102.             } else if (! is_null(Net_DNS::typesbyname(strtoupper($s)))) {
  103.                 $rrtype = strtoupper($s);
  104.                 $rdata = join(" ", $parts);
  105.                 break;
  106.             } else {
  107.                 break;
  108.             }
  109.         }
  110.  
  111.         /*
  112.          *  Do we need to do this?
  113.          */
  114.         $rdata = trim(chop($rdata));
  115.  
  116.         if (! strlen($rrtype) && strlen($rrclass) && $rrclass == "ANY") {
  117.             $rrtype = $rrclass;
  118.             $rrclass = "IN";
  119.         } else if (! strlen($rrclass)) {
  120.             $rrclass = "IN";
  121.         }
  122.  
  123.         if (! strlen($rrtype)) {
  124.             $rrtype = "ANY";
  125.         }
  126.  
  127.         if (strlen($update_type)) {
  128.             $update_type = strtolower($update_type);
  129.             if ($update_type == "yxrrset") {
  130.                 $ttl = 0;
  131.                 if (! strlen($rdata)) {
  132.                     $rrclass = "ANY";
  133.                 }
  134.             } else if ($update_type == "nxrrset") {
  135.                 $ttl = 0;
  136.                 $rrclass = "NONE";
  137.                 $rdata = "";
  138.             } else if ($update_type == "yxdomain") {
  139.                 $ttl = 0;
  140.                 $rrclass = "ANY";
  141.                 $rrtype = "ANY";
  142.                 $rdata = "";
  143.             } else if ($update_type == "nxdomain") {
  144.                 $ttl = 0;
  145.                 $rrclass = "NONE";
  146.                 $rrtype = "ANY";
  147.                 $rdata = "";
  148.             } else if (preg_match("/^(rr_)?add$/", $update_type)) {
  149.                 $update_type = "add";
  150.                 if (! $ttl) {
  151.                     $ttl = 86400;
  152.                 }
  153.             } else if (preg_match("/^(rr_)?del(ete)?$/", $update_type)) {
  154.                 $update_type = "del";
  155.                 $ttl = 0;
  156.                 $rrclass = $rdata ? "NONE" : "ANY";
  157.             }
  158.         }
  159.  
  160.         if (strlen($rrtype)) {
  161.             $this->name = $name;
  162.             $this->type = $rrtype;
  163.             $this->class = $rrclass;
  164.             $this->ttl = $ttl;
  165.             $this->rdlength = 0;
  166.             $this->rdata = "";
  167.  
  168.             if (class_exists("Net_DNS_RR_" . $rrtype)) {
  169.                 $scn = "Net_DNS_RR_" . $rrtype;
  170.                 $rc = new $scn($this, $rdata);
  171.                 return($rc);
  172.             } else {
  173.                 return($this);
  174.             }
  175.         } else {
  176.             return(NULL);
  177.         }
  178.     }
  179.  
  180.     /* }}} */
  181.     /* Net_DNS_RR::new_from_array($rrarray) {{{ */
  182.     function new_from_array($rrarray)
  183.     {
  184.         foreach ($rrarray as $k => $v) {
  185.             $this->{strtolower($k)} = $v;
  186.         }
  187.  
  188.         if (! strlen($this->name)) {
  189.             return(NULL);
  190.         }
  191.         if (! strlen($this->type)){
  192.             return(NULL);
  193.         }
  194.         if (! $this->ttl) {
  195.             $this->ttl = 0;
  196.         }
  197.         if (! strlen($this->class)) {
  198.             $this->class = "IN";
  199.         }
  200.         if (strlen($this->rdata)) {
  201.             $this->rdlength = strlen($rdata);
  202.         }
  203.         if (class_exists("Net_DNS_RR_" . $rrtype)) {
  204.             $scn = "Net_DNS_RR_" . $rrtype;
  205.             $rc = new $scn($this, $rdata);
  206.             return($rc);
  207.         } else
  208.             return($this);
  209.     }
  210.  
  211.     /* }}} */
  212.     /* Net_DNS_RR::display() {{{ */
  213.     function display()
  214.     {
  215.         echo $this->string() . "\n";
  216.     }
  217.  
  218.     /* }}} */
  219.     /* Net_DNS_RR::string() {{{ */
  220.     function string()
  221.     {
  222.         return($this->name . ".\t" . (strlen($this->name) < 16 ? "\t" : "") .
  223.                 $this->ttl  . "\t"  .
  224.                 $this->class. "\t"  .
  225.                 $this->type . "\t"  .
  226.                 $this->rdatastr());
  227.  
  228.     }
  229.  
  230.     /* }}} */
  231.     /* Net_DNS_RR::rdatastr() {{{ */
  232.     function rdatastr()
  233.     {
  234.         if ($this->rdlength) {
  235.             return("; rdlength = " . $this->rdlength);
  236.         }
  237.         return("; no data");
  238.     }
  239.  
  240.     /* }}} */
  241.     /* Net_DNS_RR::rdata() {{{ */
  242.     function rdata(&$packetORrdata, $offset = "")
  243.     {
  244.         if ($offset) {
  245.             return($this->rr_rdata($packetORrdata, $offset));
  246.         } else if (strlen($this->rdata)) {
  247.             return($this->rdata);
  248.         } else {
  249.             return(NULL);
  250.         }
  251.     }
  252.  
  253.     /* }}} */
  254.     /* Net_DNS_RR::rr_rdata($packet, $offset) {{{ */
  255.     function rr_rdata(&$packet, $offset)
  256.     {
  257.         return((strlen($this->rdata) ? $this->rdata : ""));
  258.     }
  259.     /* }}} */
  260.     /* Net_DNS_RR::data() {{{ */
  261.     function data(&$packet, $offset)
  262.     {
  263.         $data = $packet->dn_comp($this->name, $offset);
  264.         $data .= pack("n", Net_DNS::typesbyname(strtoupper($this->type)));
  265.         $data .= pack("n", Net_DNS::classesbyname(strtoupper($this->class)));
  266.         $data .= pack("N", $this->ttl);
  267.  
  268.         $offset += strlen($data) + 2;  // The 2 extra bytes are for rdlength
  269.  
  270.         $rdata = $this->rdata($packet, $offset);
  271.         $data .= pack("n", strlen($rdata));
  272.         $data .= $rdata;
  273.  
  274.         return($data);
  275.     }
  276.     /* }}} */
  277. }
  278. /* }}} */
  279. /* VIM settings {{{
  280.  * Local variables:
  281.  * tab-width: 4
  282.  * c-basic-offset: 4
  283.  * soft-stop-width: 4
  284.  * c indent on
  285.  * End:
  286.  * vim600: sw=4 ts=4 sts=4 cindent fdm=marker et
  287.  * vim<600: sw=4 ts=4
  288.  * }}} */
  289. ?>
  290.